#!/bin/bash
#
# @@@ START COPYRIGHT @@@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#
# @@@ END COPYRIGHT @@@
#

# Set the number of nodes configured
let node_count=`trafconf -nid-count`

if [[ -z ${TRAF_AGENT} ]]; then

   # Set monitor to run in agent mode
   if  [[ -n "$node_count" ]] && [[ "$node_count" -gt "1" ]]; then    
      export SQ_MON_CREATOR=MPIRUN
   fi

   if [[ "$SQ_MON_CREATOR" == "MPIRUN" ]]; then
      export SQ_MON_RUN_MODE=${SQ_MON_RUN_MODE:-AGENT}
      export MONITOR_COMM_PORT=${MONITOR_COMM_PORT:-23390}
      echo  "***"
      echo  "***" `date`  " - SQ_MON_CREATOR  = $SQ_MON_CREATOR"
      echo  "***" `date`  " - SQ_MON_RUN_MODE = $SQ_MON_RUN_MODE"
   fi

   echo  "***"
   echo  "***" `date`  " - Starting Monitor processes"
   echo  "***"
   
sqshell <<eof 

! Start the monitor processes across the cluster
startup

set SQ_MBTYPE=$SQ_MBTYPE
set JAVA_HOME=$JAVA_HOME
set TRAF_CLUSTER_ID=$TRAF_CLUSTER_ID
set TRAF_FOUNDATION_READY=0

exit
eof

else
   echo `date`" - Waiting for the Trafodion monitor process..."

   let monitor_ready=0

   # 5 seconds, iterations 240 = 20 minutes
   if sqcheckmon -s up -i 240 -d 5
   then
      let ++monitor_ready
   fi

   if [[ $monitor_ready -lt 1 ]]; then
      echo `date`" - Aborting startup!"
      cat $TRAF_LOG/sqcheckmon.log
      exit 1
   else

# Set initial registry values
sqshell -a <<eof

set SQ_MBTYPE=$SQ_MBTYPE
set JAVA_HOME=$JAVA_HOME
set TRAF_CLUSTER_ID=$TRAF_CLUSTER_ID
set TRAF_FOUNDATION_READY=0

exit
eof
   fi
fi

echo `date`" - Delaying 15 seconds before continuing with Startup"
sleep 15
echo `date`" - Continuing with Startup ..."
echo

if (
    [[ $TRAF_AGENT == "CM" ]] ||
    [[ $SQ_MON_RUN_MODE == "AGENT" ]]
   )
then
   export TRAF_SCALING_FACTOR=${TRAF_SCALING_FACTOR:-0.75}

   #echo  "***"
   #echo  "*** node_count = ${node_count}"
   #echo  "*** TRAF_SCALING_FACTOR = ${TRAF_SCALING_FACTOR}"

   # allow time for other nodes to integrate, scaled to cluster size
   # scaling factor may be non-integer, so use awk to evaluate
   start_delay=$( echo "${node_count} ${TRAF_SCALING_FACTOR}" | awk '{print $1 * $2}')
   echo  "***"
   echo  "***" %`date`" - Waiting ${start_delay} seconds for Monitor processes to integrate"
   echo  "***"
   sleep $start_delay
fi

sqshell -c node info

# Starting TSID
echo  "***"
echo  "***" `date`  " - Starting Trasaction Id Server (TSID)"
echo  "***"
idtmstart
sqshell -c delay 1

echo  "***"
echo  "***" `date`  " - Starting Distributed Transaction Manager (DTM)"
echo  "***"

tmstart
tm_ret=$?
if [ $tm_ret != 0 ]; then
   echo "DTM start failed ..."
   exit $tm_ret
fi

echo "SQ_START_RMS: " $SQ_START_RMS 
if [[ $SQ_START_RMS == "1" ]]; then
   echo "Starting RMS"
   rmsstart
   rms_start_status=$?
   if [[ $rms_start_status != 0 ]]; then
      echo "RMS start failed ..."
      exit $rms_start_status
   fi
   echo "Started RMS"
fi


echo  "***"
echo  "***" `date`  " - Trafodion Foundation Services are ready!"
echo  "***"
sqshell -a <<eof

set TRAF_FOUNDATION_READY=1

exit
eof

exit 0
